package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.User;
import com.fy56.platform.logistics.model.WeChatProgram;
import com.fy56.platform.logistics.service.web.IWeChatProgramService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Ning on 2018/5/7.
 */
@RestController
@RequestMapping("/controller/weChatProgram")
public class WeChatProgramController extends BaseController {
    @Autowired
    private IWeChatProgramService iWeChatProgramService;

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> get(Long keyId){
        WeChatProgram weChatProgram = new WeChatProgram();
        if(null != keyId){
            weChatProgram.setId(keyId);
        }
        weChatProgram = iWeChatProgramService.get(weChatProgram);
        ResultModel r = ResultModel.ok(weChatProgram);
        return  new ResponseEntity<>(r, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(WeChatProgram entity, HttpServletRequest request){
        ResultModel<ResultModel> resultModel = null;
        WeChatProgram dbModel = null;
        try{
            if (entity != null) {
                User user = SessionUtil.getSessionUser(request);
                if(null == user){
                    return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
                }

                entity.initUpdate(user);
                resultModel = iWeChatProgramService.save(entity);
                dbModel = iWeChatProgramService.get(new WeChatProgram());

                //保存系统业务日志
                LogUtils.saveLog(request,"保存成功",dbModel.getId().toString(),null,"WeChatProgramManage","save","保存", JSON.toJSONString(dbModel));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("保存失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"保存失败","",null,"WeChatProgramManage","save","保存", "");
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

}
